package routers

import (
	"git.oschina.net/fanbuchi/redpack/constant"
	"git.oschina.net/fanbuchi/redpack/domain"
	"git.oschina.net/fanbuchi/redpack/handler"
	middleware "git.oschina.net/fanbuchi/redpack/middleware/oauth2"
	nice "github.com/ekyoung/gin-nice-recovery"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

const projectName  = constant.ProjectName
//路由
func Setup() (ret *gin.Engine) {
	// Disable Console Color
	// gin.DisableConsoleColor()
	//GIN_MODE=DEBUG
	/*if env.GetFuncMode() {
		gin.SetMode(gin.ReleaseMode)
	}else {
		gin.SetMode(gin.DebugMode)
	}*/
	r := gin.New()
	r.Use(gin.Logger())
	r.Use(nice.Recovery(recoveryHandler))

	r.GET("/", func(context *gin.Context) {
		status:=domain.Status{Status:"ok",Now:time.Now()}
		context.JSON(http.StatusOK, status)
	})

	status:=r.Group(projectName+"/status")
	status.GET("/check", func(context *gin.Context) {
		status:=domain.Status{Status:"ok",Now:time.Now()}
		context.JSON(http.StatusOK, status)
	})
	// account
	accountHandler:=new(handler.AccountHandler)
	account := r.Group(projectName+"/account",gin.BasicAuth(gin.Accounts{
		"client": "secret",
	}))
	account.POST("/", accountHandler.Create)
	accountAuth := r.Group(projectName+"/account")
	accountAuth.Use(middleware.Oauth2AuthRequired())
	accountAuth.DELETE("/delete", accountHandler.Delete)
	accountAuth.GET("/query", accountHandler.QueryById)
	accountAuth.GET("/queryByUserName", accountHandler.QueryByUserName)
	accountAuth.POST("/queryByDomain", accountHandler.QueryByDomain)
	accountAuth.POST("/search", accountHandler.Search)
	//profile
	profileHandler:=new(handler.ProfileHandler)
	profileAuth := r.Group(projectName+"/profile")
	profileAuth.Use(middleware.Oauth2AuthRequired())
	profileAuth.POST("/", profileHandler.Create)
	profileAuth.DELETE("/delete", profileHandler.Delete)
	profileAuth.GET("/query", profileHandler.QueryById)
	profileAuth.GET("/queryByAccountId", profileHandler.QueryByAccountId)
	profileAuth.POST("/queryByDomain", profileHandler.QueryByDomain)
	profileAuth.POST("/search", profileHandler.Search)

	// redpack
	redPackHandler:=new(handler.RedPackHandler)
	redPack := r.Group(projectName+"/redpack",gin.BasicAuth(gin.Accounts{
		"client": "secret",
	}))
	redPack.GET("/query", redPackHandler.QueryById)
	redPack.POST("/search", redPackHandler.Search)
	redPackAuth := r.Group(projectName+"/redpack")
	redPackAuth.Use(middleware.Oauth2AuthRequired())
	redPackAuth.POST("/create", redPackHandler.Create)
	redPackAuth.DELETE("/delete", redPackHandler.Delete)

	// wallet
	walletHandler:=new(handler.WalletHandler)
	wallet := r.Group(projectName+"/wallet")

	wallet.Use(middleware.Oauth2AuthRequired())
	wallet.POST("/create", walletHandler.Create)
	wallet.DELETE("/delete", walletHandler.Delete)
	wallet.PUT("/update", walletHandler.Update)
	wallet.GET("/", walletHandler.QueryByAccountId)
	wallet.POST("/search", walletHandler.Search)

	//oauth
	oauthHandler:=new(handler.OauthHandler)
	oauth := r.Group(projectName+"/oauth")
	oauth.POST("/token", oauthHandler.Token)
	oauth.Use(middleware.Oauth2AuthRequired())
	oauth.GET("/logout", oauthHandler.Logout)

	return r
}


func recoveryHandler(c *gin.Context, err interface{}) {
	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusInternalServerError,Msg:err.(string)})
	}
}
